package com.zzl.bonds.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.bonds.ticket.constant.StringConstant;
import com.zzl.bonds.ticket.constant.status.ApproveResultStatusEnum;
import com.zzl.bonds.ticket.constant.status.TicketLogStatusEnum;
import com.zzl.bonds.ticket.constant.type.LogActionTypeEnum;
import com.zzl.bonds.ticket.constant.type.OperateLogTypeEnum;
import com.zzl.bonds.ticket.constant.type.TicketTypeSourceEnum;
import com.zzl.bonds.ticket.dao.TicketOperateLogDao;
import com.zzl.bonds.ticket.dto.TickeoperateLogAddDto;
import com.zzl.bonds.ticket.dto.TickeoperateLogUpdateDto;
import com.zzl.bonds.ticket.dto.TicketOperateLogParams;
import com.zzl.bonds.ticket.dto.feign.NoticeApprovalResult;
import com.zzl.bonds.ticket.dto.feign.TicketOperateLogAddDto;
import com.zzl.bonds.ticket.entity.*;
import com.zzl.bonds.ticket.service.*;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;
import com.zzl.general.common.utils.UuidUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.krb5.internal.Ticket;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 票的操作记录表
 *
 * @author sunpp
 * @date 2022-02-16 11:14:07
 */
@Slf4j
@Service("ticketOperateLogService")
public class TicketOperateLogServiceImpl extends ServiceImpl<TicketOperateLogDao, TicketOperateLogEntity> implements TicketOperateLogService {

    @Autowired
    private TicketService ticketService;

    @Autowired
    private TicketTransferService ticketTransferService;

    @Autowired
    private TicketFinanceService ticketFinanceService;

    @Autowired
    private TicketOpenService ticketOpenService;

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return 分页数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TicketOperateLogEntity> page = this.page(
                new Query<TicketOperateLogEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 融单
     *
     * @param ticketFinanceEntity 开单实体
     * @param user                登录用户
     */
    @Override
    public void saveTicketLog(TicketFinanceEntity ticketFinanceEntity, User user) throws SysException {
        if (ObjectUtils.isEmpty(ticketFinanceEntity)) {
            throw new SysException("实体类不能为空");
        }
        Integer ticketStatus = ticketFinanceEntity.getTicketStatus();
        if (null == ticketStatus) {
            throw new SysException("票据状态不能为空");
        }
        List<TicketOperateLogEntity> ticketOperateLogEntityList = new ArrayList<>();
        switch (ticketStatus) {
            //融单申请
            case 9:
                this.saveFinanceTicketLog(ticketFinanceEntity, user, ticketOperateLogEntityList);
        }
        //保存操作记录
        if (!this.saveBatch(ticketOperateLogEntityList)) {
            throw new SysException("保存操作记录失败");
        }
    }

    /**
     * @param ticketFinanceEntity        融单
     * @param user                       用户
     * @param ticketOperateLogEntityList 操作记录
     */
    private void saveFinanceTicketLog(TicketFinanceEntity ticketFinanceEntity, User user, List<TicketOperateLogEntity> ticketOperateLogEntityList) {
        //1 凭证融资申请单的操作记录
        TicketOperateLogEntity ticketOperateLogEntity = this.getTicketOperateLogEntity(LogActionTypeEnum.TICKET_TYPE_FINANCE.getCode(), ticketFinanceEntity.getId()
                , TicketLogStatusEnum.START.getCode(), OperateLogTypeEnum.FINANCE.getCode(), TicketLogStatusEnum.FINANCE_PENDING_REVIEW.getCode(), user
                , ticketFinanceEntity.getRemark(),
                null, null,
                ticketFinanceEntity.getEnterpriseId(), ticketFinanceEntity.getFinanceEnterpriseName(), "", "0"
                , ticketFinanceEntity.getInstinctyId(), ticketFinanceEntity.getInstinctyName());
        ticketOperateLogEntityList.add(ticketOperateLogEntity);
        //2 凭证的操作记录
        //2.1 原凭证已失效
        TicketOperateLogEntity parentLog = this.getTicketOperateLogEntity(LogActionTypeEnum.TICKET_TYPE_FINANCE.getCode(), ticketFinanceEntity.getId()
                , TicketLogStatusEnum.TRANSFER_SUCCESS.getCode(), OperateLogTypeEnum.FINANCE.getCode(), TicketLogStatusEnum.INVALID.getCode(), user
                , ticketFinanceEntity.getRemark(),
                null, null,
                ticketFinanceEntity.getEnterpriseId(), ticketFinanceEntity.getFinanceEnterpriseName(), ticketFinanceEntity.getParentTicketId(), "1"
                , ticketFinanceEntity.getInstinctyId(), ticketFinanceEntity.getInstinctyName());
        ticketOperateLogEntityList.add(parentLog);
        //2.2 新凭证开始审批
        TicketOperateLogEntity newEntity = this.getTicketOperateLogEntity(LogActionTypeEnum.TICKET_TYPE_FINANCE.getCode(), ticketFinanceEntity.getId()
                , TicketLogStatusEnum.START.getCode(), OperateLogTypeEnum.FINANCE.getCode(), TicketLogStatusEnum.FINANCE_PENDING_REVIEW.getCode(), user
                , ticketFinanceEntity.getRemark(),
                null, null,
                ticketFinanceEntity.getEnterpriseId(), ticketFinanceEntity.getFinanceEnterpriseName(), ticketFinanceEntity.getTicketId(), "1"
                , ticketFinanceEntity.getInstinctyId(), ticketFinanceEntity.getInstinctyName());
        ticketOperateLogEntityList.add(newEntity);

        //2.3 留存凭证拆转留存
        if (StringUtils.isNotBlank(ticketFinanceEntity.getRetentionTicketId())) {
            TicketOperateLogEntity childLog = this.getTicketOperateLogEntity(LogActionTypeEnum.TICKET_TYPE_FINANCE.getCode(), ticketFinanceEntity.getId()
                    , TicketLogStatusEnum.TRANSFER_SUCCESS.getCode(), OperateLogTypeEnum.FINANCE.getCode(), TicketLogStatusEnum.FINANCE_RETAINED.getCode(), user
                    , ticketFinanceEntity.getRemark(),
                    null, null,
                    ticketFinanceEntity.getEnterpriseId(), ticketFinanceEntity.getFinanceEnterpriseName(), ticketFinanceEntity.getRetentionTicketId(), "1"
                    , ticketFinanceEntity.getInstinctyId(), ticketFinanceEntity.getInstinctyName());
            ticketOperateLogEntityList.add(childLog);
        }
    }

    /**
     * 新增
     *
     * @param tickeoperateLogAddDto 新增实体
     * @param user                  当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveTicketOperateLog(TickeoperateLogAddDto tickeoperateLogAddDto, User user) throws SysException {
        TicketOperateLogEntity ticketOperateLogEntity = new TicketOperateLogEntity();
        BeanUtils.copyProperties(tickeoperateLogAddDto, ticketOperateLogEntity);
        ticketOperateLogEntity.setCreatorId(user.getId().toString());
        if (!this.save(ticketOperateLogEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param tickeoperateLogUpdateDto 修改实体
     * @param user                     当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateTickeoperateLogById(TickeoperateLogUpdateDto tickeoperateLogUpdateDto, User user) throws SysException {
        TicketOperateLogEntity ticketOperateLogEntity = new TicketOperateLogEntity();
        BeanUtils.copyProperties(tickeoperateLogUpdateDto, ticketOperateLogEntity);
        ticketOperateLogEntity.setUpdaterId(user.getId().toString());
        if (!this.updateById(ticketOperateLogEntity)) {
            throw new SysException("A00002", "修改失败！");
        }
    }

    @Override
    public void saveTicketLog(String type, String applyId, String ticketPreviousStatus, String operateType, String ticketStatus, User user,
                              String remark, String signEnterpriseId, String signEnterpriseName, String enterpriseId, String enterpriseName,
                              String ticketId, String isToApplication, String instinctyId, String instinctyName) {
        TicketOperateLogEntity logEntity = new TicketOperateLogEntity();
        String id = UuidUtils.UUID();
        logEntity.setId(id);
        logEntity.setApplyId(applyId);
        logEntity.setApplyType(type);
        logEntity.setTicketPreviousStatus(ticketPreviousStatus);
        logEntity.setOperateType(operateType);
        logEntity.setTicketStatus(ticketStatus);
        logEntity.setOperateId(user.getId().toString());
        logEntity.setOperateUser(user.getUsername());
        logEntity.setOperateTime(new Date());
        logEntity.setEnterpriseId(enterpriseId);
        logEntity.setEnterpriseName(enterpriseName);
        logEntity.setSignEnterpriseId(signEnterpriseId);
        logEntity.setSignEnterpriseName(signEnterpriseName);
        logEntity.setCreatorId(user.getId().toString());
        logEntity.setRemark(remark);
        logEntity.setTicketId(ticketId);
        logEntity.setIsToApplication(isToApplication);
        logEntity.setInstinctyId(instinctyId);
        logEntity.setInstinctyName(instinctyName);
        this.save(logEntity);
    }

    @Override
    public void saveTicketLog(String type, String applyId, String ticketPreviousStatus, String operateType, String ticketStatus, User user, String remark, String signEnterpriseId, String signEnterpriseName, String enterpriseId, String enterpriseName, String ticketId, String isToApplication, String instinctyId, String instinctyName, NoticeApprovalResult noticeApprovalResult) {
        TicketOperateLogEntity logEntity = new TicketOperateLogEntity();
        String id = UuidUtils.UUID();
        logEntity.setId(id);
        logEntity.setApplyId(applyId);
        logEntity.setApplyType(type);
        logEntity.setTicketPreviousStatus(ticketPreviousStatus);
        logEntity.setOperateType(operateType);
        logEntity.setTicketStatus(ticketStatus);
        logEntity.setOperateId(user.getId().toString());
        logEntity.setOperateUser(user.getUsername());
        logEntity.setOperateTime(new Date());
        logEntity.setEnterpriseId(enterpriseId);
        logEntity.setEnterpriseName(enterpriseName);
        logEntity.setSignEnterpriseId(signEnterpriseId);
        logEntity.setSignEnterpriseName(signEnterpriseName);
        logEntity.setCreatorId(user.getId().toString());
        logEntity.setRemark(remark);
        logEntity.setTicketId(ticketId);
        logEntity.setIsToApplication(isToApplication);
        logEntity.setInstinctyId(instinctyId);
        logEntity.setInstinctyName(instinctyName);
        logEntity.setNodeLevels(noticeApprovalResult.getNodeLevels());
        logEntity.setTemplateNodeCounts(noticeApprovalResult.getTemplateNodeCounts());
        logEntity.setApprovalResult(noticeApprovalResult.getApprovalResult());
        this.save(logEntity);
    }

    @Override
    public TicketOperateLogEntity getTicketOperateLogEntity(String type, String applyId, String ticketPreviousStatus, String operateType, String ticketStatus, User user,
                                                            String remark, String signEnterpriseId, String signEnterpriseName, String enterpriseId, String enterpriseName, String ticketId,
                                                            String isToApplication, String instinctyId, String instinctyName) {
        TicketOperateLogEntity logEntity = new TicketOperateLogEntity();
        String id = UuidUtils.UUID();
        logEntity.setId(id);
        logEntity.setApplyId(applyId);
        logEntity.setApplyType(type);
        logEntity.setTicketPreviousStatus(ticketPreviousStatus);
        logEntity.setOperateType(operateType);
        logEntity.setTicketStatus(ticketStatus);
        logEntity.setOperateId(user.getId().toString());
        logEntity.setOperateUser(user.getUsername());
        logEntity.setOperateTime(new Date());
        logEntity.setEnterpriseId(enterpriseId);
        logEntity.setEnterpriseName(enterpriseName);
        logEntity.setSignEnterpriseId(signEnterpriseId);
        logEntity.setSignEnterpriseName(signEnterpriseName);
        logEntity.setCreatorId(user.getId().toString());
        logEntity.setRemark(remark);
        logEntity.setTicketId(ticketId);
        logEntity.setIsToApplication(isToApplication);
        logEntity.setInstinctyName(instinctyName);
        logEntity.setInstinctyId(instinctyId);
        return logEntity;
    }

    @Override
    public TicketOperateLogEntity getTicketOperateLogEntity(String type, String applyId, String ticketPreviousStatus, String operateType, String ticketStatus,
                                                            User user, String remark, String signEnterpriseId, String signEnterpriseName, String enterpriseId,
                                                            String enterpriseName, String ticketId, String isToApplication, String instinctyId, String instinctyName,
                                                            NoticeApprovalResult noticeApprovalResult) {

        TicketOperateLogEntity logEntity = new TicketOperateLogEntity();
        String id = UuidUtils.UUID();
        logEntity.setId(id);
        logEntity.setApplyId(applyId);
        logEntity.setApplyType(type);
        logEntity.setTicketPreviousStatus(ticketPreviousStatus);
        logEntity.setOperateType(operateType);
        logEntity.setTicketStatus(ticketStatus);
        logEntity.setOperateId(user.getId().toString());
        logEntity.setOperateUser(user.getUsername());
        logEntity.setOperateTime(new Date());
        logEntity.setEnterpriseId(enterpriseId);
        logEntity.setEnterpriseName(enterpriseName);
        logEntity.setSignEnterpriseId(signEnterpriseId);
        logEntity.setSignEnterpriseName(signEnterpriseName);
        logEntity.setCreatorId(user.getId().toString());
        logEntity.setRemark(remark);
        logEntity.setTicketId(ticketId);
        logEntity.setIsToApplication(isToApplication);
        logEntity.setInstinctyName(instinctyName);
        logEntity.setInstinctyId(instinctyId);
        logEntity.setApprovalResult(noticeApprovalResult.getApprovalResult());
        logEntity.setTemplateNodeCounts(noticeApprovalResult.getTemplateNodeCounts());
        logEntity.setNodeLevels(noticeApprovalResult.getNodeLevels());
        return logEntity;

    }

    @Override
    public void saveFeignTicketLog(TicketOperateLogAddDto addDto, User user) throws SysException {
        TicketOperateLogEntity logEntity = new TicketOperateLogEntity();
        BeanUtils.copyProperties(addDto, logEntity);
        logEntity.setId(UuidUtils.UUID());
        if (!this.save(logEntity)) {
            throw new SysException("保存票据失败！");
        }
    }

    @Override
    public List<TicketOperateLogEntity> listOperateLog(TicketOperateLogParams params, User user) {
        List<TicketOperateLogEntity> result;
        /*
        * 1:我的票据
        * 2:转让中/转让复核
        * 3:融资中
        * 4:失效页
        * 5:开立
        * 7:开立签收
        * */
        switch (params.getType()){
            case "1" : result = listOperateLogMy(params);break;
            case "2" : result = listOperateLogReplace(params);break;
            case "3" : result = listOperateLogFinance(params);break;
            case "4" : result = listOperateLogInvalid(params);break;
            case "5" : result = /*TODO*/listOperateLogOpen(params);break;
            case "7" : result = listOperateLogOpenSign(params);break;
            default:
                result = new ArrayList<>();
        }
        this.covert(result);
        return result;
    }

    /**
     * 我的票据详情操作记录
     *
     * @param params 修改的票的操作记录表对象值
     * @return 票的操作记录
     */
    private List<TicketOperateLogEntity> listOperateLogMy(TicketOperateLogParams params) {
        List<TicketOperateLogEntity> result;
        TicketEntity ticketEntity = ticketService.getById(params.getId());
        //开立/转让签收的票
        if (TicketTypeSourceEnum.TRANSFER.getCode().equals(ticketEntity.getTicketTypeSource())) {
            QueryWrapper<TicketOperateLogEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TicketOperateLogEntity::getTicketId, params.getId());
            queryWrapper.orderByDesc("create_time");
            List<TicketOperateLogEntity> ticketOperateLogEntity = baseMapper.selectList(queryWrapper);
            result = ticketOperateLogEntity.stream().
                    filter(t -> t.getOperateType().equals(OperateLogTypeEnum.TRANSFER_SIGN.getCode())).collect(Collectors.toList());
            //补充平台和转让方或开立方操作记录
            this.supplementaryData(result, ticketEntity, ticketOperateLogEntity);

        } else if (TicketTypeSourceEnum.OPEN.getCode().equals(ticketEntity.getTicketTypeSource())) {
            QueryWrapper<TicketOperateLogEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TicketOperateLogEntity::getApplyId, ticketEntity.getTicketOpenId());
            queryWrapper.orderByDesc("create_time");
            List<TicketOperateLogEntity> ticketOperateLogEntity = baseMapper.selectList(queryWrapper);
            result = ticketOperateLogEntity.stream().
                    filter(t -> t.getOperateType().equals(OperateLogTypeEnum.SIGN.getCode())).collect(Collectors.toList());
            //补充平台和转让方或开立方操作记录
            this.supplementaryData(result, ticketEntity, ticketOperateLogEntity);
        } else {
            QueryWrapper<TicketOperateLogEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TicketOperateLogEntity::getTicketId, params.getId());
            queryWrapper.orderByDesc("create_time");
            List<TicketOperateLogEntity> ticketOperateLogEntity = baseMapper.selectList(queryWrapper);
            TicketOperateLogEntity entity = ticketOperateLogEntity.get(0);
            result = ticketOperateLogEntity.stream().filter(t -> t.getApplyId().equals(entity.getApplyId())).collect(Collectors.toList());
        }
        return result;
    }

    /**
     * 转让票据详情操作记录
     *
     * @param params 修改的票的操作记录表对象值
     * @return 票的操作记录
     */
    private List<TicketOperateLogEntity> listOperateLogReplace(TicketOperateLogParams params) {
        TicketransferEntity ticketransferEntity = ticketTransferService.getById(params.getApplyId());
        QueryWrapper<TicketOperateLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TicketOperateLogEntity::getTicketId, ticketransferEntity.getTicketId())
                .eq(TicketOperateLogEntity::getApplyId, params.getApplyId());
        queryWrapper.orderByDesc("create_time");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 融资票据详情操作记录
     *
     * @param params 修改的票的操作记录表对象值
     * @return 票的操作记录
     */
    private List<TicketOperateLogEntity> listOperateLogFinance(TicketOperateLogParams params) {
        QueryWrapper<TicketOperateLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TicketOperateLogEntity::getApplyId, params.getApplyId())
                .eq(TicketOperateLogEntity::getApplyType, LogActionTypeEnum.TICKET_TYPE_FINANCE.getCode())
                .eq(TicketOperateLogEntity::getIsToApplication, "0");
        queryWrapper.orderByDesc(StringConstant.CREATE_TIME_FILED);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 失效票据详情操作记录
     *
     * @param params 修改的票的操作记录表对象值
     * @return 票的操作记录
     */
    private List<TicketOperateLogEntity> listOperateLogInvalid(TicketOperateLogParams params) {
        QueryWrapper<TicketOperateLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TicketOperateLogEntity::getTicketId, params.getId());
        queryWrapper.orderByDesc("create_time");
        List<TicketOperateLogEntity> ticketOperateLogEntity = baseMapper.selectList(queryWrapper);
        TicketOperateLogEntity entity = ticketOperateLogEntity.get(0);
        return ticketOperateLogEntity.stream().filter(t -> t.getApplyId().equals(entity.getApplyId())).collect(Collectors.toList());
    }

    /**
     * 开立票据详情操作记录
     *
     * @param params 修改的票的操作记录表对象值
     * @return 票的操作记录
     */
    private List<TicketOperateLogEntity> listOperateLogOpen(TicketOperateLogParams params) {
        /*TODO*/
        LambdaQueryWrapper<TicketOperateLogEntity> logEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        System.out.println("-=-=-=-=-=-=-=");
        System.out.println(params.getApplyId());

        logEntityLambdaQueryWrapper.eq(TicketOperateLogEntity::getApplyId,params.getApplyId())
                .eq(TicketOperateLogEntity::getApplyType,OperateLogTypeEnum.OPEN.getCode())
                .orderByDesc(TicketOperateLogEntity::getCreateTime);
        List<TicketOperateLogEntity> logEntities = baseMapper.selectList(logEntityLambdaQueryWrapper);
        System.out.println(logEntities);
        System.out.println("=================================-------*****");
        return logEntities;
    }

    /**
     * 开立签收票据详情操作记录
     *
     * @param params 修改的票的操作记录表对象值
     * @return 票的操作记录
     */
    private List<TicketOperateLogEntity> listOperateLogOpenSign(TicketOperateLogParams params) {
        TicketOpenEntity ticketOpenEntity=ticketOpenService.getById(params.getApplyId());
        QueryWrapper<TicketOperateLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TicketOperateLogEntity::getApplyId, params.getApplyId());
        queryWrapper.orderByDesc("create_time");
        List<TicketOperateLogEntity> ticketOperateLogEntity = baseMapper.selectList(queryWrapper);
        List<TicketOperateLogEntity> result = ticketOperateLogEntity.stream().
                filter(t -> t.getOperateType().equals(OperateLogTypeEnum.SIGN.getCode())).collect(Collectors.toList());
        //补充平台和转让方或开立方操作记录
        this.supplementaryData(result, ticketOpenEntity, ticketOperateLogEntity);
        return result;
    }

    private void supplementaryData(List<TicketOperateLogEntity> result, TicketOpenEntity ticketOpenEntity, List<TicketOperateLogEntity> ticketOperateLogEntity) {
        Optional<TicketOperateLogEntity> platOperateLogEntity = ticketOperateLogEntity.stream().
                filter(t -> t.getOperateType().equals(OperateLogTypeEnum.PLAT_TICKET_APPROVE.getCode())).findFirst();
        Optional<TicketOperateLogEntity> firstOperateLogEntity = ticketOperateLogEntity.stream().
                filter(t -> t.getOperateType().equals(OperateLogTypeEnum.REVIEW_PASS.getCode())
                        && t.getTicketStatus().equals(TicketLogStatusEnum.OPEN_PENDING_APPROVE.getCode())).findFirst();
        if (platOperateLogEntity.isPresent()) {
            TicketOperateLogEntity logEntity = platOperateLogEntity.get();
            logEntity.setOperateUser("平台端");
            result.add(logEntity);
        }
        if (firstOperateLogEntity.isPresent()) {
            TicketOperateLogEntity logEntity = firstOperateLogEntity.get();
            logEntity.setTicketPreviousStatus(TicketLogStatusEnum.OPEN.getCode());
            logEntity.setOperateUser(ticketOpenEntity.getOpenEnterpriseName());
            logEntity.setNodeLevels(null);
            result.add(logEntity);
        }
    }


    private void supplementaryData(List<TicketOperateLogEntity> result, TicketEntity ticketEntity, List<TicketOperateLogEntity> ticketOperateLogEntity) {
        if (TicketTypeSourceEnum.TRANSFER.getCode().equals(ticketEntity.getTicketTypeSource())) {
            Optional<TicketOperateLogEntity> platOperateLogEntity = ticketOperateLogEntity.stream().
                    filter(t -> t.getOperateType().equals(OperateLogTypeEnum.PLAT_TICKET_APPROVE.getCode())).findFirst();
            Optional<TicketOperateLogEntity> firstOperateLogEntity = ticketOperateLogEntity.stream().
                    filter(t -> t.getOperateType().equals(OperateLogTypeEnum.REVIEW_REJECTION.getCode())
                            && t.getTicketStatus().equals(TicketLogStatusEnum.TRANSFER_PENDING_APPROVE.getCode())).findFirst();
            if (platOperateLogEntity.isPresent()) {
                TicketOperateLogEntity logEntity = platOperateLogEntity.get();
                logEntity.setOperateUser("平台端");
                result.add(logEntity);
            }
            if (firstOperateLogEntity.isPresent()) {
                TicketOperateLogEntity logEntity = firstOperateLogEntity.get();
                logEntity.setTicketPreviousStatus(TicketLogStatusEnum.TRANSFER.getCode());
                logEntity.setOperateUser(ticketEntity.getTransferEnterpriseName());
                logEntity.setNodeLevels(null);
                result.add(logEntity);
            }
        } else if (TicketTypeSourceEnum.OPEN.getCode().equals(ticketEntity.getTicketTypeSource())) {
            Optional<TicketOperateLogEntity> platOperateLogEntity = ticketOperateLogEntity.stream().
                    filter(t -> t.getOperateType().equals(OperateLogTypeEnum.PLAT_TICKET_APPROVE.getCode())).findFirst();
            Optional<TicketOperateLogEntity> firstOperateLogEntity = ticketOperateLogEntity.stream().
                    filter(t -> t.getOperateType().equals(OperateLogTypeEnum.REVIEW_PASS.getCode())
                            && t.getTicketStatus().equals(TicketLogStatusEnum.OPEN_PENDING_APPROVE.getCode())).findFirst();
            if (platOperateLogEntity.isPresent()) {
                TicketOperateLogEntity logEntity = platOperateLogEntity.get();
                logEntity.setOperateUser("平台端");
                result.add(logEntity);
            }
            if (firstOperateLogEntity.isPresent()) {
                TicketOperateLogEntity logEntity = firstOperateLogEntity.get();
                logEntity.setTicketPreviousStatus(TicketLogStatusEnum.OPEN.getCode());
                logEntity.setOperateUser(ticketEntity.getOpenEnterpriseName());
                logEntity.setNodeLevels(null);
                result.add(logEntity);
            }
        }
    }

    private void covert(List<TicketOperateLogEntity> list) {
        list.forEach(e -> {
            e.setTicketPreviousStatusName(TicketLogStatusEnum.getName(e.getTicketPreviousStatus()));
            e.setTicketStatusName(TicketLogStatusEnum.getName(e.getTicketStatus()));
            if (StringUtils.isNotBlank(e.getApprovalResult())&&e.getNodeLevels()!=null) {
                e.setOperateTypeName(OperateLogTypeEnum.getValue(
                        e.getOperateType()) + " (" + e.getNodeLevels() + "/" + e.getTemplateNodeCounts() + ") -" + ApproveResultStatusEnum.getValue(e.getApprovalResult()));
            }else if(StringUtils.isNotBlank(e.getApprovalResult())&&e.getNodeLevels()==null){
                e.setOperateTypeName(OperateLogTypeEnum.getValue(
                        e.getOperateType()) +"-" + ApproveResultStatusEnum.getValue(e.getApprovalResult()));
            }
            else {
                e.setOperateTypeName(OperateLogTypeEnum.getValue(e.getOperateType()));
            }
        });
    }
}